package edu.java.homework.hw03.ipj06;

/**
 * Realization of the visiting of elements of matrix in diagonals
 * (top-->bottom)starting from a[n-1][0]
 * 
 * @author Tsvetan Vasilev
 * 
 */

public class Exer05 {
    // Perfect implementation :) Congratulations :)
    public static void main(String[] args) {
	// int[][] matrix={{1,2,3},{4,5,6},{7,8,9}};
	// int[][] matrix={{1,2},{3,4}};

	int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
		{ 13, 14, 15, 16 }

	};

	int length = matrix.length;
	// print the array
	for (int i = 0; i < length; i++) {
	    for (int j = 0; j < length; j++) {
		System.out.print(matrix[i][j] + "\t");
	    }
	    System.out.println();
	}

	// visiting of elements in diagonal(top-->bottom) starting from
	// a[n-1][0]
	System.out.println("\n\nVisiting of the elements: ");
	for (int i = length - 1; i >= 0; i--) {
	    int k = i;
	    for (int j = 0; j <= (length - 1) - i; j++, k++) {
		System.out.print(matrix[k][j] + "\t");
	    }
	    System.out.println();
	}

	for (int j = 1; j <= length - 1; j++) {
	    int l = j;
	    for (int i = 0; i <= (length - 1) - j; i++, l++) {
		System.out.print(matrix[i][l] + "\t");
	    }
	    System.out.println();
	}

    }

}
